package com.yunduo.aspect;

import com.yunduo.common.JwtUtils;
import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.YunDuoConst.OrderQueueParam;
import com.yunduo.common.YunDuoConst.ShopCarParam;
import com.yunduo.common.YunDuoUtils;
import com.yunduo.entity.Order;
import com.yunduo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;

/**
 * 订单切面
 */
@Aspect
@Component
@Slf4j
public class OrderAspect {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;

    //订单创建后删除购物车内数据,并为订单添加超时时间
    @AfterReturning(value = "execution(* com.yunduo.controller.OrderController.createOrder())",returning = "result")
    public void deleteShopCarData(JoinPoint point, Object result) {
        CommonReturn data = (CommonReturn)result;
        Boolean success = data.getSuccess();
        User user = JwtUtils.getDataByJwtToken(YunDuoUtils.getRequest());
        if(success){
            String orderId = (String)data.getData().get("order");
            //订单超时修改
            rabbitTemplate.convertAndSend(OrderQueueParam.ORDER_DELAY_CHANGE.getValue(), OrderQueueParam.ORDER_DELAY_ROUTING_KEY.getValue(), orderId,
                    new MessagePostProcessor() {
                        @Override
                        public Message postProcessMessage(Message message) throws AmqpException {
                            message.getMessageProperties()
                                    .setDelay(1800000); //设置消息延迟时间为30分钟
                            return message;
                        }
                    });
            //删除购物车数据
            HashOperations hashOperations = redisTemplate.opsForHash();
            hashOperations.delete(ShopCarParam.SHOPCAR_REDIS,user.getId());
            //redis添加创建订单时间
            ValueOperations kv = redisTemplate.opsForValue();
            kv.set(orderId,String.valueOf(System.currentTimeMillis()),1800000, TimeUnit.MILLISECONDS);
        }
    }

    //订单状态更改后执行其他操作
    @AfterReturning(value = "execution(* com.yunduo.controller.OrderController.orderStateUpdate(..))",returning = "result")
    public void stateOperation(JoinPoint point,Object result){
        CommonReturn data = (CommonReturn)result;
        Boolean success = data.getSuccess();
        User user = JwtUtils.getDataByJwtToken(YunDuoUtils.getRequest());
        if(!success){
            return;
        }
        Order order = (Order) data.getData().get("order");
        switch (order.getState()){
            case "0":
                //待支付执行的操作
                break;
            case "1":
                //已经支付待发货
                //向商户发送邮件 提醒进行发货更改状态
                break;
            case "2":
                //已发货
                break;
            case "3":
                //待收货
                break;
            case "4":
                //已收货
                break;
            case "5":
                //完成订单
                break;
            case "6":
                //取消订单
                break;
        }
    }

}
